import axios from "axios"
import { Toast } from "antd-mobile"

const request=axios.create({
    timeout:5000
})

request.interceptors.request.use(config => {
    // token
    const token = localStorage.getItem('token')

    // 将loading打开
    Toast.show({
        content: '数据请求中。。。。',
        duration: 0,
        position: 'top',
    })

    if (token) {
        config.headers = {
            ...config.headers,
            Authorization: token
        }
    }
    return config
})

// 响应拦截
request.interceptors.response.use(config => {
    // 响应正常

    // 将loading关掉
    Toast.clear()

    return config.data
}, (err) => {
    // 响应异常
    const { status } = err.response
    
    Toast.clear()

    // 轻提示
    Toast.show({
        icon: 'fail',
        content: err.response.data.msg,
    })

    // 处理错误状态
    if (status === 500) {
        window.location.href = '/404'
    }
    return Promise.reject(err)
})
export default request